﻿using System;
using System.Threading.Tasks;
using Magicodes.WeChat.Application.QRCode.Dto;
using Magicodes.WeChat.SDK;
using Magicodes.WeChat.SDK.Apis.QRCode;
using Abp.Json;

namespace Magicodes.WeChat.Application.QRCode
{
    /// <summary>
    /// 微信二维码相关
    /// </summary>
    public class WeChatQRCodeAppService : AppServiceBase, IWeChatQRCodeAppService
    {
        /// <summary>
        /// 创建二维码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public Task<QRCodeCreateApiResult> CreateQrCode(CreateQRCodeInput input)
        {
            var value = string.IsNullOrWhiteSpace(input.Key) ? input.Value : input.ToJsonString();
            var result = WeChatApisContext.Current.QrCodeApi.
                CreateByStringValue(value, expireSeconds: input.ExpireSeconds.HasValue ? input.ExpireSeconds.Value : 259200).HanderApiResultError();
            return Task.FromResult(result);
        }

        /// <summary>
        /// 根据ticket获取二维码链接
        /// </summary>
        /// <param name="ticket"></param>
        /// <returns></returns>
        public Task<string> GetQrCodeUrl(string ticket)
        {
            return Task.FromResult(WeChatApisContext.Current.QrCodeApi.GetQrCodeUrl(ticket));
        }
    }
}
